import { GCellRenderer } from "../../../../com/mui/controls/GCellRenderer";
import { PetPracticeConst } from "./PetPracticeConst";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { EventName } from "../../mvc/EventName";
import { PetPracticeEventName } from "./PetPracticeEventName";
import { SPetInfo } from "../../../../Message/Game/SPetInfo";
import { PetUtil } from "../pet/info/PetUtil";
import { Language } from "../../../../extend/language/Language";
import { EPetStatus } from "../../../../Message/Game/EPetStatus";
import { Cache } from "../../cache/Cache";
import { SecTimer } from "../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../com/gengine/core/frame/TimerType";
import { PetPracticeOper } from "./PetPracticeOper";
import { RemainTimeTool } from "../award/RemainTimeTool";
import { ObjCreate } from "../common/ObjCreate";
import { ImagesConst } from "../../resource/ImagesConst";
import { PetItem } from "../pet/PetItem";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { ResouceConst } from "../../../component/gconst/ResouceConst";
import { NumInput } from "../common/NumInput";
import { EPetTrainType } from "../../../../Message/Game/EPetTrainType";
import { BuyAlert } from "../pet/BuyAlert";
import { EPrictUnit } from "../../../../Message/Public/EPrictUnit";
import { DataEvent } from "../../events/DataEvent";
import { MsgManager } from "../../manager/MsgManager";
import { PetTrainConfig } from "../../resource/PetTrainConfig";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { ScaleBitmap } from "../../../../com/mui/display/ScaleBitmap";
import { GButton } from "../../../../com/mui/controls/GButton";
import { _petInfo } from "../../../../this/_petInfo";
import { _operType } from "../../../../this/_operType";
type int = number;
//class PetPracticeRenderer
    
    export  class PetPracticeRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            this._addPercent = PetPracticeConst.addPercent / 100;
            this.init();
            this.initListener();
            return;
        }

        /* internal  */onTimeChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this.updateExpTxt();
            this._operBtn.toolTipData = this.getOperBtnTip();
            return;
        }

        /* internal  */initListener(): void
        {
            Dispatcher.addEventListener(EventName.PetPracticeSuccess, this.onPetPracticeSuccessHandler);
            Dispatcher.addEventListener(EventName.PetPracticeAccSuccess, this.onPetPracticeAccSuccessHandler);
            Dispatcher.addEventListener(EventName.PetGetTrainInfoSuccess, this.onPetGetTrainInfoSuccessHandler);
            Dispatcher.addEventListener(PetPracticeEventName.PracticeModeChange, this.onPracticeModeChangeHandler);
            Dispatcher.addEventListener(PetPracticeEventName.PracticeAccModeChange, this.onPracticeAccModeChangeHandler);
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            if (arg1) 
            {
                this._petInfo = arg1 as SPetInfo;
                this._petItem.dragSource = this._petInfo;
                this._nameTxt.htmlText = PetUtil.getPetName(this._petInfo);
                this._levelTxt.text = this._petInfo.level + Language.getString(54216);
                this._isPracticing = this._petInfo.status == EPetStatus._EpetTrain;
                this._isPracticeComplete = this._petInfo.totalTrainTime >= 24;
                this._isHigh = this._petInfo.level >= Cache.instance.role.entityInfo.level + 10;
                if (this._isPracticing) 
                {
                    this._statusTxt.text = Language.getString(54285);
                    if (this._secTimer == null) 
                    {
                        this._secTimer = new SecTimer();
                    }
                    this._secTimer.addListener(TimerType.ENTERFRAME, this.onTimerUpdateHandler);
                    this._secTimer.addListener(TimerType.COMPLETE, this.onTimerCompleteHandler);
                    this._secTimer.repeatCount = this._leftSec;
                    this._secTimer.reset();
                    this._secTimer.start();
                    this._operType = PetPracticeOper.Accelerate;
                    this._operBtn.label = Language.getString(54392);
                    this._timeInput.visible = false;
                    this.updateExpTxt();
                }
                else 
                {
                    if (this._secTimer) 
                    {
                        this._secTimer.stop();
                        this._timeTxt.text = "";
                    }
                    if (PetUtil.isBattled(this._petInfo)) 
                    {
                        this._statusTxt.text = Language.getString(54284);
                        this._timeTxt.text = Language.getString(54393);
                        this._timeInput.visible = false;
                        this._operBtn.visible = false;
                    }
                    else 
                    {
                        this._statusTxt.text = Language.getString(54394);
                        this._trainMode = 0;
                        if (this._isPracticeComplete) 
                        {
                            this._timeTxt.text = Language.getString(54395);
                            this._expTxt.text = "";
                            this._addExpTxt.text = "";
                            this._timeInput.visible = false;
                            this._operBtn.visible = false;
                        }
                        else if (this._isHigh) 
                        {
                            this._timeTxt.text = Language.getString(54396);
                            this._expTxt.text = "";
                            this._addExpTxt.text = "";
                            this._timeInput.visible = false;
                            this._operBtn.visible = false;
                        }
                        else 
                        {
                            this._timeInput.visible = true;
                            this.updateExpTxt();
                            this._operBtn.label = Language.getString(54397);
                            this._operBtn.visible = true;
                            this._timeTxt.text = "";
                            this._operType = PetPracticeOper.Practice;
                        }
                    }
                }
                if (this._isHigh) 
                {
                    this._timeTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
                }
                else 
                {
                    this._timeTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
                }
                this._hourLabel.visible = this._timeInput.visible;
                this._operBtn.toolTipData = this.getOperBtnTip();
                this._bakPetInfo = this._petInfo;
            }
            return;
        }

        /* internal  */onTimerUpdateHandler(arg1: SecTimer): void
        {
            var loc1;
            var loc2=((loc1 = this)._leftSec - 1);
            loc1._leftSec = loc2;
            this._timeTxt.text = RemainTimeTool.getRemainTimeBySeconds(this._leftSec);
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: SecTimer): void
        {
            this._secTimer.stop();
            return;
        }

        /* internal  */init(): void
        {
            ObjCreate.createBg(14, 16, 38, 38, this, ImagesConst.PackItemBg);
            this._petItem = new PetItem();
            this._petItem.isDropAble = false;
            this._petItem.isDragAble = false;
            this._petItem.width = 32;
            this._petItem.height = 32;
            this._petItem.move(16, 18);
            this.addChild(this._petItem);
            this._nameTxt = ObjCreate.createTextField("", 60, 20, 70, 20, this);
            this._nameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._levelTxt = ObjCreate.createTextField("", 60, 43, 70, 20, this, GlobalStyle.textFormat2);
            this._levelTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._overSkin = ResouceConst.getScaleBitmap(ImagesConst.Friend_over);
            this._overSkin.width = 110;
            this._overSkin.height = 43;
            this._statusTxt = ObjCreate.createTextField("", 148, 27, 50, 20, this);
            this._statusTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._timeTxt = ObjCreate.createTextField("", 205, 27, 115, 20, this);
            this._timeTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._timeInput = new NumInput();
            this._timeInput.minNum = 1;
            this._timeInput.maxNum = 24;
            this._timeInput.currentNum = 1;
            this._timeInput.x = 215;
            this._timeInput.y = 27;
            this._timeInput.addEventListener(flash.events.Event.CHANGE, this.onTimeChangeHandler);
            this.addChild(this._timeInput);
            this._hourLabel = ObjCreate.createTextField(Language.getString(54398), 305, 27, 60, 20, this);
            this._expTxt = ObjCreate.createTextField("", 340, 20, 75, 20, this);
            this._expTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._addExpTxt = ObjCreate.createTextField("", 340, 40, 75, 20, this);
            this._addExpTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._operBtn = ObjCreate.createGButton(Language.getString(54392), 410, 25, 60, 24, this);
            this._operBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onOPerBtnClickHandler);
            this._operBtn.drawNow();
            ObjCreate.createBg(10, 68, 450, 2, this, ImagesConst.SplitLine);
            return;
        }

        /* internal  */onOPerBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            if (this._petInfo) 
            {
                if (this._practiceMode == EPetTrainType._EPetTrainTypeGold || this._operType == PetPracticeOper.Accelerate) 
                {
                    if (!BuyAlert.checkMoney(EPrictUnit._EPriceUnitGold, this._needCost)) 
                    {
                        return;
                    }
                }
                else 
                {
                    loc1 = Cache.instance.role.money;
                    if (loc1.coin + loc1.coinBind < this._needCost) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, this._needCost));
                        return;
                    }
                }
                if (this._petInfo.level >= Cache.instance.role.entityInfo.level + 10) 
                {
                    MsgManager.addBroadCast(Language.getString(54399));
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(PetPracticeEventName.PracticeOper, {"petInfo": _petInfo, "operType": _operType, "hour": this._timeInput.currentNum}));
            }
            return;
        }

        /* internal  */onPetPracticeSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["hour"] as int;
            if (this._petInfo.uid == loc1) 
            {
                this._leftSec = loc2 * 3600;
                this._petInfo.status = EPetStatus._EpetTrain;
                this._trainMode = this._practiceMode;
                this.data = this._petInfo;
                MsgManager.showRollTipsMsg(Language.getString(54400));
                this._operBtn.toolTipData = this.getOperBtnTip();
                this.updateExpTxt();
            }
            return;
        }

        /* internal  */onPetPracticeAccSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["remainSeconds"] as int;
            if (this._petInfo.uid == loc1) 
            {
                this._leftSec = loc2;
                this._timeInput.maxNum = 24 - this._petInfo.totalTrainTime;
                this._timeInput.currentNum = 1;
                this.data = this._petInfo;
                MsgManager.showRollTipsMsg(Language.getString(54401));
                this._operBtn.toolTipData = this.getOperBtnTip();
                this.updateExpTxt();
            }
            return;
        }

        /* internal  */onPetGetTrainInfoSuccessHandler(arg1: DataEvent): void
        {
            this._practicePetDict = arg1.data as Map<any, any> /* flash.utils.Dictionary */;
            var loc1=this._practicePetDict[this._petInfo.uid];
            if (loc1) 
            {
                this._leftSec = loc1.remainSeconds;
                this._trainMode = loc1.type;
                this.data = this._petInfo;
            }
            return;
        }

        /* internal  */getExpectExp(arg1: SPetInfo): int
        {
            var loc1=NaN;
            if (arg1) 
            {
                this._phExp = PetTrainConfig.instance.getExpByLevel(arg1.level);
                loc1 = int(this._phExp * this._timeInput.currentNum);
                if (this._trainMode != EPetTrainType._EPetTrainTypeGold) 
                {
                    if (this._trainMode != EPetTrainType._EPetTrainTypeCoin) 
                    {
                        if (this._practiceMode == EPetTrainType._EPetTrainTypeGold) 
                        {
                            loc1 = loc1 + this.getAddExpectExp(arg1);
                        }
                    }
                    else 
                    {
                        loc1 = int(this._phExp * this._timeInput.currentNum);
                    }
                }
                else 
                {
                    loc1 = loc1 + this.getAddExpectExp(arg1);
                }
            }
            return loc1;
        }

        /* internal  */getAddExpectExp(arg1: SPetInfo): int
        {
            var loc1=NaN;
            if (arg1) 
            {
                loc1 = int(this._phExp * this._timeInput.currentNum * this._addPercent);
            }
            return loc1;
        }

        /* internal  */updateExpTxt(): void
        {
            this._expTxt.text = this.getExpectExp(this._petInfo).toString();
            this._addExpTxt.text = "（+" + this.getAddExpectExp(this._petInfo) + "）";
            if (this._trainMode != EPetTrainType._EPetTrainTypeGold) 
            {
                if (this._trainMode != EPetTrainType._EPetTrainTypeCoin) 
                {
                    if (this._practiceMode != EPetTrainType._EPetTrainTypeGold) 
                    {
                        this._expTxt.y = 27;
                        this._addExpTxt.visible = false;
                    }
                    else 
                    {
                        this._expTxt.y = 20;
                        this._addExpTxt.visible = true;
                    }
                }
                else 
                {
                    this._expTxt.y = 27;
                    this._addExpTxt.visible = false;
                }
            }
            else 
            {
                this._expTxt.y = 20;
                this._addExpTxt.visible = true;
            }
            return;
        }

        /* internal  */onPracticeModeChangeHandler(arg1: DataEvent): void
        {
            this._practiceMode = arg1.data as int;
            this._operBtn.toolTipData = this.getOperBtnTip();
            this.updateExpTxt();
            if (PetUtil.isBattled(this._petInfo) || this._isPracticeComplete || this._isHigh) 
            {
                this._expTxt.visible = false;
                this._addExpTxt.visible = false;
            }
            else 
            {
                this._expTxt.visible = true;
            }
            return;
        }

        /* internal  */onPracticeAccModeChangeHandler(arg1: DataEvent): void
        {
            this._accMode = arg1.data as int;
            this._operBtn.toolTipData = this.getOperBtnTip();
            return;
        }

        /* internal  */getOperBtnTip(): string
        {
            var loc1=null;
            if (this._operType != PetPracticeOper.Practice) 
            {
                loc1 = Language.getString(54405);
                if (this._accMode != 0) 
                {
                    if (this._accMode != 1) 
                    {
                        this._needCost = PetPracticeConst.accQuarterGold * 4;
                        loc1 = loc1 + Language.getString(54411);
                        loc1 = loc1 + Language.getStringByParam(54409, this._needCost);
                    }
                    else 
                    {
                        this._needCost = PetPracticeConst.accQuarterGold;
                        loc1 = loc1 + Language.getString(54410);
                        loc1 = loc1 + Language.getStringByParam(54409, this._needCost);
                    }
                }
                else 
                {
                    this._needCost = this.getQuickCompleteCost();
                    loc1 = loc1 + Language.getString(54408);
                    loc1 = loc1 + Language.getStringByParam(54409, this._needCost);
                }
            }
            else 
            {
                loc1 = Language.getStringByParam(54402, this._timeInput.currentNum);
                if (this._practiceMode != EPetTrainType._EPetTrainTypeGold) 
                {
                    this._needCost = this._timeInput.currentNum * PetPracticeConst.hourCoin;
                    loc1 = loc1 + Language.getStringByParam(54404, this._needCost);
                }
                else 
                {
                    this._needCost = this._timeInput.currentNum * PetPracticeConst.hourGold;
                    loc1 = loc1 + Language.getStringByParam(54403, this._needCost, this._addPercent * 100);
                }
            }
            return HTMLUtil.addColor(loc1, "#ffffff");
        }

        /* internal  */getQuickCompleteCost(): int
        {
            var loc1=0;
            if (this._leftSec % 900 != 0) 
            {
                loc1 = int(this._leftSec / 900) + 1;
            }
            else 
            {
                loc1 = this._leftSec / 900;
            }
            return loc1;
        }

        /* internal  */isEqualBackPetInfo(arg1: SPetInfo): Boolean
        {
            var loc1=false;
            if (arg1 && this._bakPetInfo) 
            {
                loc1 = arg1.uid == this._bakPetInfo.uid && arg1.name == this._bakPetInfo.name && arg1.level == this._bakPetInfo.level && arg1.status == this._bakPetInfo.status && arg1.totalTrainTime == this._bakPetInfo.totalTrainTime;
            }
            return loc1;
        }

        /* internal */ /* var */_phExp: number;

        /* internal */ /* var */_addPercent: number;

        /* internal */ /* var */_practiceMode: int=1;

        /* internal */ /* var */_statusTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_trainMode: int;

        /* internal */ /* var */_isHigh: Boolean;

        /* internal */ /* var */_bakPetInfo: SPetInfo;

        /* internal */ /* var */_needCost: int;

        /* internal */ /* var */_accMode: int=0;

        /* internal */ /* var */_petItem: PetItem;

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_levelTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_overSkin: ScaleBitmap;

        /* internal */ /* var */_timeTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_expTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_addExpTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_operBtn: GButton;

        /* internal */ /* var */_timeInput: NumInput;

        /* internal */ /* var */_hourLabel: Object /* flash.text.TextField */;

        /* internal */ /* var */_petInfo: SPetInfo;

        /* internal */ /* var */_leftSec: number=0;

        /* internal */ /* var */_operType: int;

        /* internal */ /* var */_secTimer: SecTimer;

        /* internal */ /* var */_isPracticing: Boolean;

        /* internal */ /* var */_isPracticeComplete: Boolean;

        /* internal */ /* var */_practicePetDict: Map<any, any> /* flash.utils.Dictionary */;
    }
